package com.qingfeng.client.service;

import com.qingfeng.client.bean.Goods;
import com.qingfeng.client.dao.GoodsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2021/1/19 下午7:55
 */
@Slf4j
@Service
public class ActivityService {

    // 送给领导的赠品
    private static String giftKey="gift";

    @Autowired
    private GoodsMapper goodsMapper;

    @Resource
    private RedisTemplate redisTemplate;

    private Set<String> getCodes(String goodsCommonId){
        return  redisTemplate.opsForSet().members(giftKey+goodsCommonId);
    }

    public void removeCode(String code,String goodsId){
        try {
            Goods goods= goodsMapper.selectById(goodsId);
            redisTemplate.opsForSet().remove(giftKey+goods.getGoodsCommonId(),0,code);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void addCode(String code,String goodsId){
        try {
            Goods goods= goodsMapper.selectById(goodsId);
            redisTemplate.opsForSet().add(giftKey+goods.getGoodsCommonId(),code);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            log.error(code);
        }

    }


    public boolean checkCode(String code,String goodsId){
        Set<String> codes = getCodes(goodsId);
        if (codes.contains(code)){
            return true;
        }else {
            return false;
        }
    }





}
